package com.eryanet.gamedemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import com.eryanet.gamesdk.EryaGameSDK;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.view.MotionEvent.ACTION_CANCEL;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_p)
    TextView tvP;
    @BindView(R.id.btn_select)
    Button btnSelect;
    @BindView(R.id.btn_start)
    Button btnStart;
    @BindView(R.id.btn_a)
    Button btnA;
    @BindView(R.id.btn_b)
    Button btnB;
    @BindView(R.id.btn_c)
    Button btnC;
    @BindView(R.id.btn_x)
    Button btnX;
    @BindView(R.id.btn_y)
    Button btnY;
    @BindView(R.id.btn_Z)
    Button btnZ;
    @BindView(R.id.btn_up)
    Button btnUp;
    @BindView(R.id.btn_down)
    Button btnDown;
    @BindView(R.id.btn_left)
    Button btnLeft;
    @BindView(R.id.btn_right)
    Button btnRight;
    @BindView(R.id.btn_join)
    Button btnJoin;
    @BindView(R.id.layout_fun)
    ConstraintLayout layoutFun;
    @BindView(R.id.btn_lefttop)
    Button btnLefttop;
    @BindView(R.id.btn_righttop)
    Button btnRighttop;
    @BindView(R.id.btn_leftbottom)
    Button btnLeftbottom;
    @BindView(R.id.btn_rightbottom)
    Button btnRightbottom;

    private String ip;
    private int port;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initTouch();
        EryaGameSDK.getInstance().init(this);
        EryaGameSDK.getInstance().registConnectListener(new EryaGameSDK.ConnectListener() {
            @Override
            public void connectSuccess() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        layoutFun.setVisibility(View.VISIBLE);
                    }
                });

            }

            @Override
            public void disconnect() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        layoutFun.setVisibility(View.INVISIBLE);
                    }
                });
            }

            @Override
            public void fuctionKeyCallback(List<String> keyList) {

            }

            @Override
            public void directionKeyCallback(List<String> directionList) {

            }

            @Override
            public void connectedPerson(int count) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvP.setText(count + "P");
                    }
                });
            }

            @Override
            public void mainPlayerInfoCallback(String ip, int port) {
                Log.e("linan", "22222 ip = " + ip + "  port = " + port);
                MainActivity.this.ip = ip;
                MainActivity.this.port = port;
            }
        });
        EryaGameSDK.getInstance().startConnect();
        btnJoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //22222 test ip的ip = 192.168.157.37  port = 20119
                EryaGameSDK.getInstance().setJoinResult("192.168.47.194", 20119);
            }
        });
    }

    private void initTouch() {
        btnSelect.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameOption("select", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameOption("select", "release");
                }
                return false;
            }
        });
        btnStart.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameOption("start", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameOption("start", "release");
                }
                return false;
            }
        });

        btnA.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameOption("A", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameOption("A", "release");
                }
                return false;
            }
        });
        btnB.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameOption("B", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameOption("B", "release");
                }
                return false;
            }
        });
        btnC.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameOption("C", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameOption("C", "release");
                }
                return false;
            }
        });
        btnX.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameOption("X", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameOption("X", "release");
                }
                return false;
            }
        });
        btnY.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameOption("Y", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameOption("Y", "release");
                }
                return false;
            }
        });
        btnZ.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameOption("Z", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameOption("Z", "release");
                }
                return false;
            }
        });
        btnDown.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameDirection("bottom", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameDirection("bottom", "release");
                }
                return false;
            }
        });
        btnLeft.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameDirection("left", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameDirection("left", "release");
                }
                return false;
            }
        });
        btnUp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameDirection("top", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameDirection("top", "release");
                }
                return false;
            }
        });
        btnRight.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameDirection("right", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameDirection("right", "release");
                }
                return false;
            }
        });
        btnLefttop.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameDirection("topLeft", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameDirection("topLeft", "release");
                }
                return false;
            }
        });
        btnLeftbottom.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameDirection("bottomLeft", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameDirection("bottomLeft", "release");
                }
                return false;
            }
        });
        btnRighttop.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameDirection("topRight", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameDirection("topRight", "release");
                }
                return false;
            }
        });
        btnRightbottom.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case ACTION_DOWN:
                        EryaGameSDK.getInstance().callGameDirection("bottomRight", "press");
                        break;
                    case ACTION_MOVE:
                        break;
                    case ACTION_UP:
                    case ACTION_CANCEL:
                        EryaGameSDK.getInstance().callGameDirection("bottomRight", "release");
                }
                return false;
            }
        });


    }
}